package com.five.author.controller;

import com.five.Page.Page;
import com.five.Page.PageList;
import com.five.author.service.AuthorService;
import com.five.exception.BusinessException;
import com.five.pojo.Author;
import com.five.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/author")
@CrossOrigin
public class AuthorController {

    @Autowired
    private AuthorService authorService;

    @PostMapping("/insert")
    public Result<Author> insertAuthor(@RequestBody Author author) {
        try {
            authorService.insert(author);
            return Result.success("添加成功");
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return Result.error("添加失败");
        }
    }

    @PostMapping("/deleteById/{id}")
    public Result<Author> deleteAuthorById(@PathVariable Long id) {
        try {
            authorService.deleteById(id);
            return Result.success("删除成功");
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    @PostMapping("/update")
    public Result<Author> updateAuthor(@RequestBody Author author) {
        try {
            authorService.updateById(author);
            return Result.success("修改成功");
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return Result.error("修改失败");
        }
    }

    @PostMapping("/selectById/{id}")
    public Result<Author> selectAuthorById(@PathVariable Long id) {
        try {
            Author author = authorService.selectById(id);
            return Result.success(author);
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    @PostMapping("/selectByPage")
    public Result<PageList<Author>> selectAuthorByPage(@RequestBody Page<Author> page) {
        try {
            PageList<Author> pageList = authorService.selectByPage(page);
            return Result.success(pageList);
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    @PostMapping("/selectAll")
    public Result<List<Author>> selectAllAuthor() {
        try {
            List<Author> list = authorService.selectAll();
            return Result.success(list);
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 章节审核
     * @param chapterIds
     * @return
     */
    @PostMapping("/chapterAudit")
    public Result<String> chapterAudit(@RequestBody List<Long> chapterIds) {
        try {
            authorService.chapterAudit(chapterIds);
            return Result.success("审核成功");
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return Result.error("审核失败");
        }
    }

}
